package com.lap.service.starter.tools;

import com.lap.service.starter.dto.DeviceDto;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
import jakarta.servlet.http.HttpServletRequest;

import java.util.Optional;

/**
 * 设备工具
 *
 * @author Shuisheng Lao(劳水生)
 * @version 0.0.1
 */
public final class DeviceUtil {

  private DeviceUtil() {}

  public static DeviceDto getDevice(HttpServletRequest request) {
    String agent = request.getHeader("User-Agent");
    UserAgent userAgent = UserAgent.parseUserAgentString(agent);

    Version nullValue = new Version("UNKNOWN", "0", "0");
    Version version = Optional.ofNullable(userAgent.getBrowserVersion()).orElse(nullValue);

    DeviceDto deviceDto = new DeviceDto();
    deviceDto.setBrowser(userAgent.getBrowser().getName());
    deviceDto.setBrowserVersion(version.getVersion());
    deviceDto.setOperatingSystem(userAgent.getOperatingSystem().getName());
    return deviceDto;
  }
}
